package mfuny.ggongal.admin.report;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.json.JsonCreater;

import org.json.simple.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class LoginReportService {
	SqlMapClient sqlMapper;
	
	ModelAndView mav;
	
	public LoginReportService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		mav = new ModelAndView();
	}
	
	//접속자 수 리포트 페이지.
	public ModelAndView loginReport() throws Exception {
		mav.clear( );
		mav.setViewName( "/reports/loginReport.ga" );
		return mav;
	}
	
	//리포트 검색 내용 출력(날짜별).
	@SuppressWarnings( { "deprecation" } )
	public ModelAndView loginReportDaySearch(HttpServletRequest request) throws Exception {
		String searchType = request.getParameter( "searchType" );
		JsonCreater jc = new JsonCreater();
		JSONObject json = new JSONObject();
		List<String> sum = new ArrayList<String>();
		for ( int i = 0; i < 24; i++ ) {
			String hourColumn = "h" + i;
			sum.add( hourColumn );
		}
		
		if ( searchType.equals( "전일" )) {
			Date yesterday = new Date();
			yesterday.setDate( yesterday.getDate( ) - 1 );
			String today = new SimpleDateFormat( "yyyy-MM-dd" ).format( yesterday );
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put( "today", today );
			paramMap.put( "sum", sum );
			json = jc.getJsonByList( jc.getDaoList( "login_selectOneDayByMap", paramMap ), "loginList" );
		} else if ( searchType.equals( "전월" )) {
			Date startDate = new Date();
			startDate.setMonth( startDate.getMonth( ) - 1 );
			startDate.setDate( 1 );
			
			Date endDate = new Date();
			endDate.setDate( 1 );
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startDay = sdf.format( startDate );
			String endDay = sdf.format( endDate );
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put( "startDay", startDay );
			paramMap.put( "endDay", endDay );
			paramMap.put( "sum", sum );
			json = jc.getJsonByList( jc.getDaoList( "login_selectOneMonth", paramMap ), "loginList" );
		} else if ( searchType.equals( "당일" )) {
			Date yesterday = new Date();
			yesterday.setDate( yesterday.getDate( ) );
			String today = new SimpleDateFormat( "yyyy-MM-dd" ).format( yesterday );
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put( "today", today );
			paramMap.put( "sum", sum );
			json = jc.getJsonByList( jc.getDaoList( "login_selectOneDayByMap", paramMap ), "loginList" );
		} else if ( searchType.equals( "당월" )) {
			Date startDate = new Date();
			startDate.setDate( 1 );
			
			Date endDate = new Date();
			endDate.setMonth( endDate.getMonth( ) + 1 );
			endDate.setDate( 1 );
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startDay = sdf.format( startDate );
			String endDay = sdf.format( endDate );
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put( "startDay", startDay );
			paramMap.put( "endDay", endDay );
			paramMap.put( "sum", sum );
			json = jc.getJsonByList( jc.getDaoList( "login_selectOneMonth", paramMap ), "loginList" );
		}
		
		mav.clear( );
		mav.setViewName( "/reports/loginReportDetail" );
		mav.addObject( "loginList", json.toJSONString( ) );
		mav.addObject( "searchType", searchType );
		return mav;
	}
	
	//리포트 검색 내용 출력(시간대별).
	public ModelAndView loginReportTimeSearch( HttpServletRequest request ) throws Exception {
		String date = request.getParameter( "date" );
		JsonCreater jc = new JsonCreater();
		JSONObject json = new JSONObject();
		List<String> sum = new ArrayList<String>();
		for ( int i = 0; i < 24; i++ ) {
			String hourColumn = "h" + i;
			sum.add( hourColumn );
		}
		
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put( "today", date );
		paramMap.put( "sum", sum );
		json = jc.getJsonByList( jc.getDaoList( "login_selectOneDayByMap", paramMap ), "loginList" );
		
		mav.clear( );
		mav.setViewName( "/reports/loginReportDetail2" );
		mav.addObject( "loginList", json.toJSONString( ) );
		mav.addObject( "date", date );
		return mav;
	}
}
